package com.xiaotu.factory.simplefactory.pizzastore.order;

import com.xiaotu.factory.simplefactory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author 张晓
 * @create 2020-06-19 10:25
 */
public class OrderPizza {

//    public OrderPizza(){
//
//        Pizza pizza = null;
//        String orderType;//订购披萨的类型
//        do{
//            orderType = getType();
//            if (orderType.equals("greek")) {
//                pizza = new GreekPizza("希腊披萨");
//            }else if (orderType.equals("cheese")){
//                pizza = new CheesePizza("奶酪披萨");
//            }else if(orderType.equals("pepper")){
//                pizza = new PepperPizza("胡椒披萨");
//            }else {
//                break;
//            }
//            //输出披萨制作过程
//            pizza.prepare();
//            pizza.bake();
//            pizza.cut();
//            pizza.box();
//        }while (true);
//
//    }

    //构造器
    public OrderPizza(SimpleFactory simpleFactory){
        this.setFactory(simpleFactory);
    }


    //定义一个简单工厂对象
    SimpleFactory simpleFactory;
    Pizza pizza = null;
    public void setFactory(SimpleFactory simpleFactory){
        String orderType = "";//用户输入

        this.simpleFactory = simpleFactory;//设置简单工厂对象
        do{
            orderType = getType();
            pizza = this.simpleFactory.createPizza(orderType);
            //输出pizza
            if (pizza != null) {//订购成功
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
                System.out.println(" 订购披萨失败 ");
                break;
            }
        }while (true);
    }

    //写一个方法，可以获取客户希望订购披萨的类型
    private String getType(){
        String str = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println(" input pizza type: ");
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        finally {
//            if (br != null) {
//                try {
//                    br.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
        return str;
    }

}
